#!/bin/bash

# chkconfig: 35 99 10
# description: Cloud Agent

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well

. /etc/rc.d/init.d/functions

# set environment variables

SHORTNAME=`basename $0`
PIDFILE=@PIDDIR@/"$SHORTNAME".pid
LOCKFILE=@LOCKDIR@/"$SHORTNAME"
LOGFILE=@AGENTLOG@
PROGNAME="Cloud Agent"
CLASS="com.cloud.agent.AgentShell"
JSVC=`which jsvc 2>/dev/null`;

# exit if we don't find jsvc
if [ -z "$JSVC" ]; then
    echo no jsvc found in path;
    exit 1;
fi

unset OPTIONS
[ -r @SYSCONFDIR@/sysconfig/"$SHORTNAME" ] && source @SYSCONFDIR@/sysconfig/"$SHORTNAME"

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/jre /usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-openjdk-i386 /usr/lib/jvm/java-6-openjdk-amd64 /usr/lib/jvm/java-6-sun /usr/lib/jvm/java-1.5.0-sun /usr/lib/j2sdk1.5-sun /usr/lib/j2sdk1.5-ibm /usr/lib/jvm/jre-1.7.0"

jhome=""
for jdir in $JDK_DIRS; do
    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
        jhome="$jdir"
    fi
done
if [ ! -z $jhome ];then
export JAVA_HOME="$jhome"
fi

SCP="@SYSTEMCLASSPATH@"
DCP="@DEPSCLASSPATH@"
ACP="@AGENTCLASSPATH@"
JCP="/usr/share/java/commons-daemon.jar"

# We need to append the JSVC daemon JAR to the classpath
# AgentShell implements the JSVC daemon methods
export CLASSPATH="$SCP:$DCP:$ACP:$JCP:@AGENTSYSCONFDIR@:@AGENTLIBDIR@"

start() {
        echo -n $"Starting $PROGNAME: "
	if hostname --fqdn >/dev/null 2>&1 ; then
		$JSVC -cp "$CLASSPATH" -pidfile "$PIDFILE" -errfile SYSLOG $CLASS
		RETVAL=$?
		echo
	else
		failure
		echo
		echo The host name does not resolve properly to an IP address.  Cannot start "$PROGNAME". > /dev/stderr
		RETVAL=9
	fi
	[ $RETVAL = 0 ] && touch ${LOCKFILE}
	return $RETVAL
}

stop() {
	echo -n $"Stopping $PROGNAME: "
	$JSVC -pidfile "$PIDFILE" -stop $CLASS
	RETVAL=$?
	echo
	[ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE}
}


# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status -p ${PIDFILE} $SHORTNAME
	RETVAL=$?
	;;
  restart)
	stop
	sleep 3
	start
	;;
  condrestart)
	if status -p ${PIDFILE} $SHORTNAME >&/dev/null; then
		stop
		sleep 3
		start
	fi
	;;
  *)
	echo $"Usage: $SHORTNAME {start|stop|restart|condrestart|status|help}"
	RETVAL=3
esac

exit $RETVAL
